﻿
using System;
using System.Drawing;
using System.Windows.Forms;

using c_FormMover;
using n_GFilePanel;
using n_GUIset;
using n_LaterFilesManager;
using n_OS;
using s_Win32;
using n_UserModule;

namespace v_GForm
{
public partial class GForm : Form
{
	FormMover fm;
	GFilePanel myGFilePanel;
	
	const int MovePadding = 5;
	const int TopPadding = 5;
	const int LeftPadding = 5;
	const int RightPadding = 5;
	const int BottomPadding = 5;
	
	string AFileName;
	
	//主窗口
	public GForm( string StartFileName )
	{
		InitializeComponent();
		
		this.Padding = new Padding( LeftPadding, TopPadding, RightPadding, BottomPadding );
		
		//防止最大化之后挡住任务栏
		this.MaximizedBounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
		
		//this.BackColor = GUIset.myFreeAreaColor;
		this.BackColor = GUIset.FormColor;
		//this.BackColor = Color.Gray;
		
		this.Text = OS.USER;
		
		fm = new FormMover( this );
		
		Point p0 = new Point(
			Screen.PrimaryScreen.WorkingArea.Width/2 - this.Width/2,
			Screen.PrimaryScreen.WorkingArea.Height/2 - this.Height/2 );
		this.Location = p0;
		
		AFileName = StartFileName;
		
		//添加默认程序文件
		if( AFileName == null ) {
			AFileName = n_StartFile.StartFile.SFile;
		}
	}
	
	protected override void WndProc(ref Message m)
	{
		if(m.Msg == (int)WinMsg.WM_NCPAINT) {
			return;
		}
		else if(m.Msg == (int)WinMsg.WM_NCCALCSIZE) {
			return;
		}
		else if(m.Msg == (int)WinMsg.WM_NCACTIVATE) {
			if (m.WParam == (IntPtr)0) {
				m.Result = (IntPtr)1;
			}
		}
		else if(m.Msg == (int)WinMsg.WM_NCHITTEST) {
			bool CanResize = true;
			if(CanResize == true) {
				Point point = new Point(m.LParam.ToInt32());
				int x = point.X;
				int y = point.Y;
				if (x <= this.Left + MovePadding && y <= this.Top + MovePadding) {
					m.Result = (IntPtr)HitTest.HTTOPLEFT;
				}
				else if(x >= this.Right - MovePadding && y <= this.Top + MovePadding) {
					m.Result = (IntPtr)HitTest.HTTOPRIGHT;
				}
				else if(x >= this.Right - MovePadding && y >= this.Bottom - MovePadding) {
					m.Result = (IntPtr)HitTest.HTBOTTOMRIGHT;
				}
				else if(x <= this.Left + MovePadding && y >= this.Bottom - MovePadding) {
					m.Result = (IntPtr)HitTest.HTBOTTOMLEFT;
				}
				else if(x <= this.Left + MovePadding) {
					m.Result = (IntPtr)HitTest.HTLEFT;
				}
				else if(y <= this.Top + MovePadding) {
					m.Result = (IntPtr)HitTest.HTTOP;
				}
				else if(x >= this.Right - MovePadding) {
					m.Result = (IntPtr)HitTest.HTRIGHT;
				}
				else if(y >= this.Bottom - MovePadding) {
					m.Result = (IntPtr)HitTest.HTBOTTOM;
				}
				else {
					m.Result = (IntPtr)HitTest.HTCAPTION;
				}
			}
			else {
				//m.Result = (IntPtr)Win32.HTCAPTION;
				Win32API.ReleaseCapture();
				int SC_MOVE = 0xF012;
				Win32API.SendMessage(this.Handle, (int)WinMsg.WM_SYSCOMMAND, (int)(SC_MOVE + HitTest.HTCAPTION), (IntPtr)0);

			}
		}
		else {
			base.WndProc(ref m);
		}
	}
	
	void OpenFile()
	{
		//打开文件对话框
		OpenFileDialog OpenFileDlg = new OpenFileDialog();
		OpenFileDlg.Filter = n_CCode.CCode.Filter;
		OpenFileDlg.Title = "请选择文件";
		
		DialogResult dlgResult = OpenFileDlg.ShowDialog();
		if(dlgResult == DialogResult.OK) {
			OpenFile( false, OpenFileDlg.FileName );
		}
	}
	
	void NewFile()
	{
		//保存文件对话框
		SaveFileDialog SaveFileDlg = new SaveFileDialog();
		SaveFileDlg.Filter = n_CCode.CCode.Filter;
		SaveFileDlg.Title = "保存文件";
		
		SaveFileDlg.FileName = "我的小作品";
		DialogResult dlgResult = SaveFileDlg.ShowDialog();
		if(dlgResult == DialogResult.OK) {
			string FilePath = SaveFileDlg.FileName;
			if( !FilePath.ToLower().EndsWith( "." + OS.LinkboyFile ) ) {
				FilePath += "." + OS.LinkboyFile;
			}
			System.IO.File.Copy(  n_StartFile.StartFile.SFile, FilePath );
			
			//创建文档
			OpenFile( false, FilePath );
		}
	}
	
	void SaveAsFile()
	{
		//保存文件对话框
		SaveFileDialog SaveFileDlg = new SaveFileDialog();
		SaveFileDlg.Filter = n_CCode.CCode.Filter;
		SaveFileDlg.Title = "文件另存为...";
		
		SaveFileDlg.FileName = "我的小作品";
		DialogResult dlgResult = SaveFileDlg.ShowDialog();
		if(dlgResult == DialogResult.OK) {
			string FilePath = SaveFileDlg.FileName;
			if( !FilePath.ToLower().EndsWith( "." + OS.LinkboyFile ) ) {
				FilePath += "." + OS.LinkboyFile;
			}
			RichTextBox t = new RichTextBox();
			t.SelectAll();
			t.SelectedText = "";
			
			
			G.ccode.GUItext = myGFilePanel.GPanel.VarToText();
			
			G.ccode.SetCode( G.ccode.UserText + G.ccode.GUItext );
			
			VIO.SaveTextFile( FilePath, G.ccode.GetCode() );
			
			//创建文档
			OpenFile( true, FilePath );
			
			MessageBox.Show( "当前文档已经关闭, 其内容已经另存为新文档, 您现在编辑的文档为另存后的文档:" + FilePath, "文档编辑提示" );
		}
	}
	
	//打开新文件
	void OpenFile( bool IgnoreSave, string FileName )
	{
		//判断上一个是否需要关闭
		if( myGFilePanel != null ) {
			if( !IgnoreSave && !myGFilePanel.TryClose() ) {
				return;
			}
			this.Controls.Remove( myGFilePanel );
		}
		//添加到近期文档列表
		if( FileName != n_StartFile.StartFile.SFile ) {
			LaterFilesManager.AddFile( FileName );
		}
		//创建一个新的图形控件
		myGFilePanel = new GFilePanel( FileName, this.Width, this.Height );
		G.CGPanel = myGFilePanel.GPanel;
		G.CGPanel.GHead.SizeChanged( null, null );
		
		myGFilePanel.GPanel.GHead.DeleNewFile = NewFile;
		myGFilePanel.GPanel.GHead.DeleOpenFile = OpenFile;
		myGFilePanel.GPanel.GHead.DeleSaveAsFile = SaveAsFile;
		
		myGFilePanel.GPanel.GHead.TargetForm = this;
		
		this.Controls.Clear();
		this.Controls.Add( myGFilePanel );
		G.CGPanel.Invalidate();
	}
	
	//窗体关闭事件
	void GFormFormClosing(object sender, FormClosingEventArgs e)
	{
		if( myGFilePanel != null ) {
			if( !myGFilePanel.TryClose() ) {
				e.Cancel = true;
				return;
			}
		}
	}
	
	//窗体尺寸改变事件
	void GFormResize(object sender, EventArgs e)
	{
		if( myGFilePanel != null ) {
			myGFilePanel.GPanel.Invalidate();
		}
	}
	
	//窗体加载
	void GFormLoad(object sender, EventArgs e)
	{
		//判断是否双击程序本身打开
		if( AFileName != null ) {
			OpenFile( true, AFileName );
		}
	}
}
}

